import { GButton } from "../../../../../com/mui/controls/GButton";
import { FreezingEffect } from "../FreezingEffect";
import { SecTimer } from "../../../../../com/gengine/core/frame/SecTimer";
import { TimerType } from "../../../../../com/gengine/core/frame/TimerType";
import { CDItemData } from "../../../cache/CDItem/CDItemData";
type int = number;
//class TimeButton
    
    export  class TimeButton extends GButton
    {
        constructor(arg1: int=5, arg2: Boolean=true, arg3: string="countDown")
        {
            super();
            this.timeCount = arg1;
            this.isClickTrigger = arg2;
            this.type = arg3;
            this.addListeners();
            return;
        }

        public set isClickTrigger(arg1: Boolean)
        {
            this._isClickTrigger = arg1;
            return;
        }

        public set timeCount(arg1: int)
        {
            this._timeCount = arg1;
            if (this._timer) 
            {
                this.repeatCount = this._timeCount;
            }
            return;
        }

        public set repeatCount(arg1: int)
        {
            this._repeatCount = arg1;
            if (this._timer) 
            {
                this._timer.repeatCount = arg1;
                if (this._timer.running) 
                {
                    super.label = arg1.toString();
                }
            }
            return;
        }

        /* internal  */set type(arg1: string)
        {
            this._type = arg1;
            if (this._type != TimeButton.COUNDDOWN) 
            {
            };
            if (this._type == TimeButton.CD && !this._freezingEffect) 
            {
                this._freezingEffect = new FreezingEffect();
                this._freezingEffect.setMaskSize(33, 22);
            }
            return;
        }

        /* internal  */addListeners(): void
        {
            this.addEventListener(flash.events.MouseEvent.CLICK, this.clickHandler);
            return;
        }

        public clickHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            if (this._isClickTrigger) 
            {
                this.trigger();
            }
            return;
        }

        public trigger(): void
        {
            if (this._type == TimeButton.COUNDDOWN) 
            {
                this.triggerByCoundDown();
            }
            if (this._type == TimeButton.CD) 
            {
                this.triggerByCD();
            }
            return;
        }

        /* internal  */triggerByCoundDown(): void
        {
            if (!this._timer) 
            {
                this._timer = new SecTimer(1, this._timeCount);
                this._timer.addListener(TimerType.ENTERFRAME, this.showTextHandler);
                this._timer.addListener(TimerType.COMPLETE, this.complete_Handler);
            }
            this._timer.start();
            this._triggerTime = flash.utils.getTimer();
            super.label = this._timer.repeatCount.toString();
            this.enabled = false;
            return;
        }

        /* internal  */showTextHandler(arg1: SecTimer): void
        {
            super.label = this._timer.repeatCount.toString();
            return;
        }

        /* internal  */complete_Handler(arg1: SecTimer): void
        {
            var loc1=flash.utils.getTimer();
            var loc2=this._timeCount * 1000 + this._triggerTime - loc1;
            if (loc2 <= 0) 
            {
                this.clearCountDown();
            }
            else 
            {
                flash.utils.setTimeout(this.clearCountDown, loc2);
            }
            return;
        }

        /* internal  */triggerByCD(): void
        {
            if (!this._cdItemData) 
            {
                this._cdItemData = new CDItemData();
            }
            this._cdItemData.totalTime = this._timeCount;
            this._freezingEffect.cdTime = this._cdItemData;
            this.enabled = false;
            this._freezingEffect.x = this.width / 2;
            this._freezingEffect.y = this.height / 2;
            this.addChild(this._freezingEffect);
            this._cdItemData.startCoolDown();
            this._freezingEffect.addEventListener(flash.events.Event.COMPLETE, this.completeHandler);
            return;
        }

        /* internal  */completeHandler(arg1: Object /* flash.events.Event */): void
        {
            this.enabled = true;
            if (this._freezingEffect.parent) 
            {
                this._freezingEffect.parent.removeChild(this._freezingEffect);
            }
            return;
        }

        public get running(): Boolean
        {
            if (this._type == TimeButton.COUNDDOWN) 
            {
                return this._timer && this._timer.running;
            }
            if (this._type == TimeButton.CD) 
            {
                return this._freezingEffect && this._freezingEffect.isCDPlaying;
            }
            return false;
        }

        public clearCountDown(): void
        {
            if (this._timer) 
            {
                this._timer.stop();
                this._timer.dispose();
                this._timer = null;
                super.label = this._templabel;
                this.enabled = true;
            }
            return;
        }

        public clearCD(): void
        {
            this._cdItemData.stopCoolDown();
            this.enabled = true;
            if (this._freezingEffect.parent) 
            {
                this._freezingEffect.stop();
                this._freezingEffect.parent.removeChild(this._freezingEffect);
            }
            return;
        }

        public /* override */ set label(arg1: string)
        {
            this._templabel = arg1;
            super.label = arg1;
            return;
        }

        public /* override */ dispose(arg1: Boolean=true): void
        {
            this.clearCD();
            this.clearCountDown();
            this.timeCount = 5;
            this.isClickTrigger = true;
            this._triggerTime = 0;
            this.type = TimeButton.COUNDDOWN;
            super.dispose(arg1);
            return;
        }

        public static /* const */COUNDDOWN: string="countDown";

        public static /* const */CD: string="cd";

        /* internal */ /* var */_timeCount: int;

        /* internal */ /* var */_repeatCount: int;

        /* internal */ /* var */_timer: SecTimer;

        /* internal */ /* var */_templabel: string="";

        /* internal */ /* var */_isClickTrigger: Boolean;

        /* internal */ /* var */_freezingEffect: FreezingEffect;

        /* internal */ /* var */_cdItemData: CDItemData;

        /* internal */ /* var */_type: string;

        /* internal */ /* var */_triggerTime: int;
    }
